﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.bill
{
    public partial class AddInMoney : BaseAdmin
    {
        readonly dalCompany theCompany = new dalCompany();
        readonly DALInMoney theInMoney = new DALInMoney();
        tbInMoney oInMoney;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.InMoneyAdd);
                this.BindCompanyList();
                this.BuildForm();
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                decimal.Parse(this.txtMoney.Text.Trim());
            }
            catch
            {
                base.Response.Write("<script language='javascript'>alert('金额只能为数字！');</script>");
                return;
            }
            oInMoney = new tbInMoney();
            oInMoney.Money = decimal.Parse(this.txtMoney.Text.Trim());
            oInMoney.Remark = re.replace(this.txtRemark.Text);
            oInMoney.AdminId = int.Parse(base.AdminUserId);
            oInMoney.CompanyId = int.Parse(this.ddlCompany.SelectedItem.Value);
            oInMoney.AddTime = DateTime.Now;
            theInMoney.Add(oInMoney);
            base.Response.Write("<script language='javascript'>alert('保存成功！');window.location.href='InMoneyList.aspx';</script>");
        }

        void BindCompanyList()
        {
            DataSet ds = theCompany.GetList("");
            this.ddlCompany.DataTextField = "name";
            this.ddlCompany.DataValueField = "id";
            this.ddlCompany.DataSource = ds;
            this.ddlCompany.DataBind();
            this.ddlCompany.Items.Insert(0, new ListItem("请选择公司名称...", ""));
        }


        void BuildForm()
        {
            //if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            //{
            //    oInMoney = theInMoney.GetModel(int.Parse(Request.QueryString["id"]));
            //    this.txtMoney.Text = oInMoney.Money.ToString();
            //    this.txtRemark.Text = oInMoney.Remark;
            //    if (this.ddlCompany.Items.FindByValue(oInMoney.CompanyId.ToString()).Selected != null)
            //    {
            //        this.ddlCompany.Items.FindByValue(oInMoney.CompanyId.ToString()).Selected = true;
            //    }
            //}

        }
    }
}